/**
 * 自动引入interface目录下所有文件的内容暴露
 * @author william
 * @date 2025-7-5 11:35:32
 *
 * @type {*}
 */
const modulesFiles = import.meta.glob('./**/*.js', { eager: true })

const apiInterface = {}
for (const [key, apiValue] of Object.entries(modulesFiles)) {
  const moduleName = key.match(/.*\/(.+).js$/)[1]
  apiInterface[moduleName] = apiValue
}

/**
 * 返回的接口数据与本地数据格式对比并存储
 */
export const apiDataFormat = (basicData, formatData) => {
  const data = {}
  for (const [key, value] of Object.entries(formatData)) {
    const basicValue = basicData[key] || ''

    if (Object.prototype.toString.call(value) === '[object Array]') {
      if (basicValue && basicValue.length) {
        data[key] = basicValue.map(item => apiDataFormat(item, value[0]))
      } else {
        data[key] = []
      }
    } else if (Object.prototype.toString.call(value) === '[object Object]') {
      data[key] = apiDataFormat(basicValue, value)
    } else {
      data[key] = basicValue !== 0 && !basicValue ? value : basicValue
    }
  }
  return data
}

apiInterface.apiDataFormat = apiDataFormat

export { apiInterface }
